package com.scs.application.modules.dept.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.consts.GlobalConsts;
import com.scs.application.core.mvc.controller.FlowController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.modules.dept.entity.Demand;
import com.scs.application.modules.dept.entity.DemandItem;
import com.scs.application.modules.dept.service.DemandItemService;
import com.scs.application.modules.dept.service.DemandService;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.sys.model.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 */
@RestController
@RequestMapping("/dept/demand")
public class DemandController extends FlowController<DemandService, Demand> {


    @Autowired
    private DemandItemService demandItemService;
    /**
     * 转调拨单
     * @param demandAutos
     * @return
     */
    @PostMapping("toDemandSum")
    public RestResult toDemandSum(@RequestBody List<Demand> demandAutos) {
        return RestResult.ok("OK", service.toDemandSum(demandAutos));
    }



    /**
     * 根据ID查找数据
     */
    @GetMapping("/find")
    public RestResult find(@RequestParam("id") Serializable id) {
        Demand entity = service.getById(id);
        if (entity == null) {
            return RestResult.error("数据查询失败<br>可能所操作数据已被删除，请刷新后重新尝试操作");
        }
        entity.setCountDtl(demandItemService.count(Wrappers.<DemandItem>query().select("id").eq("demand_id",entity.getId())));

        return RestResult.ok("获取数据成功", entity);
    }


    /**
    * @author Tai
    * @Description 需求汇总选择使用
    * @Date 2024/4/18 上午9:32
    */
    @GetMapping("/pageForSumSelect")
    public PageResult pageForSumSelect() {
        QueryContext queryContext = QueryContextHolder.currentContext();
        queryContext.getWrapper().eq("flow_status", GlobalConsts.FLOW_SUBMIT_VALUE);
        queryContext.getWrapper().notInSql("id", " SELECT ifnull(demand_id,'') FROM dept_demand_sum_item ");
        IPage pageResult = service.page(queryContext);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }
}
